﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using University.Workshop.Model;

namespace University.Workshop.DataAccess
{
    public class DataAccessAllotment
    {
        public static List<Model.Allotment> SearchAllotmentsNotAssigned(Model.Allotment allotment)
        {
            WFPEntities.Instance.DiscardChanges();
            return
                (from v in WFPEntities.Instance.Allotment
                 where v.AllotmentName == allotment.AllotmentName
                 && v.SquareId == allotment.SquareId
                 && v.KardexId == null
                 select v).ToList();
        }

        public static List<Model.Allotment> SearchActiveAllotments()
        {
            WFPEntities.Instance.DiscardChanges();
            return (from a in WFPEntities.Instance.Allotment
                    where a.Enable == true && a.KardexId.HasValue
                    select a).ToList();
        }

        public static List<Model.Allotment> SearchAllotmentByNameAndSquareId(Model.Allotment allotment)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from a in WFPEntities.Instance.Allotment
                        where a.AllotmentName==allotment.AllotmentName && 
                        a.SquareId==allotment.SquareId 
                    select a).ToList();
        }

        public static void InsertNewAllotment(Model.Allotment allotment)
        {
            WFPEntities.Instance.DiscardChanges();
            WFPEntities.Instance.Allotment.Add(allotment);
            WFPEntities.Instance.SaveChanges();
        }

        public static List<Model.Allotment> SearchAllotmentBySquareId(Model.Square square)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from a in WFPEntities.Instance.Allotment
                    where a.SquareId == square.SquareId 
                    select a).ToList();
        }



        public static List<Model.Allotment> SearchAllotmentsNotAssignedToKardex(Model.Kardex mp)
        {
            WFPEntities.Instance.DiscardChanges();
            return
                (from v in WFPEntities.Instance.Allotment
                 where v.AllotmentName == mp.AllotmentModel.AllotmentName
                 && v.SquareId == mp.AllotmentModel.SquareId
                 && (v.KardexId == null||v.KardexId==mp.KardexId)
                 select v).ToList();
        }

        public static void DeleteKardexFromAllotment(Model.Allotment allotment)
        {
            WFPEntities.Instance.DiscardChanges();
            var q = (from v in WFPEntities.Instance.Allotment
                     where v.AllotmentId == allotment.AllotmentId
                     select v).First();
            q.KardexId = null;
            q.Kardex = null;
            WFPEntities.Instance.SaveChanges();
        }

        public static void DeleteKardexFromAllotment(Model.Kardex modifyKardex)
        {
            WFPEntities.Instance.DiscardChanges();
            var p = (from v in WFPEntities.Instance.Allotment
                     where v.KardexId == modifyKardex.KardexId
                     select v).ToList();
            if (p.Count != 0)
            {
                var q = p.First();
            q.KardexId = null;
            q.Kardex = null;
            WFPEntities.Instance.SaveChanges();
        }
    }

        public static void AssignAllotmentToKardex(Model.Kardex modifyKardex)
        {

            try
            {
                Model.Allotment q = (from k in WFPEntities.Instance.Allotment
                                  where k.AllotmentId == modifyKardex.AllotmentModel.AllotmentId
                                  select k).First();
                q.KardexId = modifyKardex.KardexId;
                WFPEntities.Instance.SaveChanges();
            }
            catch
            {
                WFPEntities.Instance.DiscardChanges();
                Model.Allotment q = (from k in WFPEntities.Instance.Allotment
                                     where k.AllotmentId == modifyKardex.AllotmentModel.AllotmentId
                                     select k).First();
                q.KardexId = modifyKardex.KardexId;
                WFPEntities.Instance.SaveChanges();
            }
        }

        public static ICollection<Model.Allotment> SearchAllotmentsOcupedInSquare(Model.Square square)
        {

            WFPEntities.Instance.DiscardChanges();
            return
                (from v in WFPEntities.Instance.Allotment
                 where v.SquareId == square.SquareId && v.KardexId!=null
                 select v).ToList();
        }

        public static ICollection<Model.Allotment> SearchAllotmentInSquareOcuped(Model.Allotment allotment, Model.Square square)
        {

            WFPEntities.Instance.DiscardChanges();
            var al =
                SearchAllotmentByNameAndSquareId(new Allotment()
                                                     {
                                                         AllotmentName = allotment.AllotmentName,
                                                         SquareId = square.SquareId
                                                     });
            if(al.Any())
            {
                var a = al.First();

                var q=
                    (from v in WFPEntities.Instance.Kardex
                     where
                         (from a1 in v.Allotment
                              where a1.AllotmentId==a.AllotmentId
                              select a1).Count()!=0
                     select v).ToList();
                if (q.Count != 0)
                    return al;
            }

                return new Collection<Allotment>();
        }

        public static ICollection<Allotment> SearchAllotmentByNameAndSquare(Allotment modifyAllotment, Square modifySquare)
        {
            WFPEntities.Instance.DiscardChanges();
            return
                (from v in WFPEntities.Instance.Allotment
                 where v.AllotmentName == modifyAllotment.AllotmentName && v.SquareId==modifySquare.SquareId
                 select v).ToList();
        }

        public static void ChangeStateToDisableAllotment(Allotment allotment)
        {
            WFPEntities.Instance.DiscardChanges();
            Model.Allotment q = (from k in WFPEntities.Instance.Allotment
                                 where k.AllotmentId == allotment.AllotmentId
                                 select k).First();
            if(q.Enable)
            q.Enable = !q.Enable;
            WFPEntities.Instance.SaveChanges();
        }

        public static void ChangeStateToEnableAllotment(Allotment modifyAllotment, Square modifySquare)
        {
            WFPEntities.Instance.DiscardChanges();
            Model.Allotment q = (from k in WFPEntities.Instance.Allotment
                                 where k.AllotmentId == modifyAllotment.AllotmentId &&
                                 k.SquareId==modifySquare.SquareId
                                 select k).First();
            if (!q.Enable)
                q.Enable = !q.Enable;
            WFPEntities.Instance.SaveChanges();
        }


        public static ICollection<Allotment> SearchAllotmentBySquareIdActive(Square square)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from a in WFPEntities.Instance.Allotment
                    where a.SquareId == square.SquareId&&
                    a.Enable
                    select a).ToList();
        }
    }
}
